package com.itic.mall.mobile.notice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.marketnotice.persistence.model.MarketNotice;
import com.itic.mall.system.marketnotice.persistence.model.MarketNoticeExample;
import com.itic.mall.system.marketnotice.persistence.model.MarketNoticeExample.Criteria;
import com.itic.mall.system.marketnotice.service.MarketNoticeService;


/**
 * 用户登入接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/notice")
public class MallNoticeInterface
     extends BaseMobileInterface {
   
	@Autowired
	private MarketNoticeService marketNoticeService;
    /**
     * APP展示所有的公告信息
     * @return
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public Map<String,Object> in(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		
    		MarketNoticeExample example = new MarketNoticeExample();
    		example.setOrderByClause("CREATE_TIME DESC");
    		example.setOrderByClause("REORDER ASC");
    		example = this.handlePageQuery(request, example);
    		Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		
    		List<MarketNotice> list = marketNoticeService.list(example);
    		resultMap.put(DATA, list);
    		result = SUCCESS;
			errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallNoticeInterface", "list", e.toString(),"公告通知列表",e);
    		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
    
    
    /**
     * 根据ID显示一条公告信息
     * @return
     */
    @RequestMapping(value="/detail")
    @ResponseBody
    public Map<String,Object> detail( HttpServletRequest request,HttpServletResponse response, String id ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(id) ){
    			MarketNotice notice = marketNoticeService.get(id);
    			if( null != notice ){
    				resultMap.put(DATA, notice);
        			result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = FAIL;
        			errorCode = NOTICE_ID_ERROR_CODE;
                	errorMsg = NOTICE_ID_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallNoticeInterface", "detail", e.toString(),"公告详情",e);
    		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }

    
    
}